//
//  FansTableViewController.m
//  SeaMelonSeeds_Counselor
//
//  Created by 尤振宇 on 2017/8/9.
//  Copyright © 2017年 杨笑. All rights reserved.
//

#import "FansTableViewController.h"
#import "FansTableViewCell.h"
#import "FansApi.h"
#import "SendMessageViewController.h"

@interface FansTableViewController ()

@end

@implementation FansTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.bounces = YES;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.tableView registerCellWithNib:@"FansTableViewCell"];
    [self configNotDataView:[UIImage imageNamed:@"noFans"] text:@"暂无粉丝"];
    self.title = @"粉丝 (0)";
    
    [self addMj_footer];
    [self addMj_header];
    [self loadData];
    // Do any additional setup after loading the view.
}

-(void)loadData
{
    if (!self.listApi) {
        self.listApi = [[FansApi alloc]init];
    }
    WeakSelf(self)
    [self.listApi startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        
        if (kCode(request.responseJSONObject)) {
            if (weakself.listApi.page == 1) {
                [weakself.dataArray removeAllObjects];
            }
            
            self.title = [NSString stringWithFormat:@"粉丝 (%@)",[[request.responseJSONObject objectForKey:@"data"] objectForKey:@"total"]];
            NSArray *arr = [[request.responseJSONObject objectForKey:@"data"] objectForKey:@"list"];
            for (NSDictionary *dic in arr) {
                Fans *obj = [[Fans alloc]init];
                [obj setValuesForKeysWithDictionary:dic];
                [weakself.dataArray addObject:obj];
            }
        }
        [self showOrHideDataView];
        [weakself.tableView reloadData];
        [weakself endRefreshing];
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        [weakself endRefreshing];
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    FansTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FansTableViewCell" forIndexPath:indexPath];
    
    Fans *fans = self.dataArray[indexPath.row];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:fans.avatar] placeholderImage:[UIImage imageNamed:@"My_TheDefaultAvatar"]];
    cell.title.text = fans.nickname;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 55;
}

-(void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)section
{
    SendMessageViewController *vc = [[SendMessageViewController alloc]initWithConversationChatter:@"" conversationType:(EMConversationTypeChat)];
    [self pushViewController:vc animated:YES];
}



@end
